//
//  UIPhoneInputView.swift
//  test-ios
//
//  Created by wyz5204 on 2023/3/12.
//

import UIKit

class UIPhoneInputView: UIAbstractView {

    private lazy var line : UIView = {
        return UIView().then { view in
            view.backgroundColor = UIColor.separatorLine
        }
    }()
    
    
    private lazy var leftLabel: UILabel = {
        let view = UILabel()
        view.text = "+86"
        view.font = UIFont.systemFont(ofSize: 14)
        view.textColor = UIColor.black
        return view
    }()
    
    lazy var textField: UITextField = {
        let view = UITextField()
        let font = UIFont.systemFont(ofSize: 15)
        let color = UIColor.green
        view.attributedPlaceholder = NSAttributedString(string: "请输入手机号",
                                                        attributes: [.font: font, .foregroundColor: color])
        view.text = "13019710660"
        view.keyboardType = .numberPad
        view.clearButtonMode = .whileEditing
        view.returnKeyType = .done
        return view
    }()
    
    lazy var vCodeBtn: JKCountDownButton = {
        let view = JKCountDownButton(type: .custom)
        view.setTitle("获取验证码", for: .normal)
        view.setTitleColor(UIColor.gl_theme, for: .normal)
        view.setTitleColor(UIColor.gl_black, for: .disabled)
        view.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        return view
    }()
    
    
    override func initView(_ frame: CGRect) {
        
        // 添加+86
        leftLabel.addTo(self) { view in
            view.snp.makeConstraints { (make) in
                make.centerY.equalToSuperview()
                make.left.equalToSuperview()
            }
        }
        
        // 验证码倒计时
        vCodeBtn.addTo(self) { view in
            view.setCountDownChanging { bt, time in
                return "还剩\(time)s"
            }
            
            view.setCountDownFinished { bt, time in
                return "获取验证码"
            }
            
            view.setCountDownButtonHandler { bt, time in
                print(" setCountDownFinished >> \(bt) >>  \(time) ")
                bt.startCountDownWithSecond(totalSecond: 10)
            }
            view.snp.makeConstraints { (make) in
                make.right.equalToSuperview()
                make.centerY.equalToSuperview()
                make.width.equalTo(85)
                make.height.equalTo(28)
            }
        }
        
        // 添加手机输入框
        textField.addTo(self) { view in
            view.snp.makeConstraints { (make) in
                make.centerY.equalToSuperview()
                make.left.equalToSuperview().offset(52)
                make.right.equalTo(vCodeBtn.snp.left).offset(-10)
            }
        }
        
        line.addTo(self) { view in
            view.snp.makeConstraints { (make) in
                make.width.equalToSuperview()
                make.height.equalTo(1)
                make.bottom.equalToSuperview()
            }
        }
    }

}
